﻿#include "IdaDialogMsg.h"
#include "ui_IdaDialogMsg.h"

#include <QScreen>
#include <QDebug>

#include "IdaMainWindow.h"
#include "IdaWidgetHelper.h"
#include "AutoResizingTextBrowser.h"

static QPointer<IdaDialogMsg> m_dialogMsgGlobal = nullptr;

IdaDialogMsg::IdaDialogMsg(QWidget* parent)
    : QDialog(parent)
    , m_windowFrame(new IdaMainWindow(parent)), m_textBrower_content(new AutoResizingTextBrowser)
{
    ui = new Ui::IdaDialogMsg();
    ui->setupUi(this);

    initDialog();
}

IdaDialogMsg::~IdaDialogMsg()
{
    delete ui;
    ui = nullptr;
}

/// @brief 设置窗体

void IdaDialogMsg::initDialog()
{
    //m_windowFrame->setMaximumSize(320, 170);
    //m_windowFrame->setFixedWidth(300);
    m_windowFrame->setMaximumHeight(400);
    m_windowFrame->setCentralWidget(this);
    //m_windowFrame->setWindowTitle(title);
    m_windowFrame->setWindowModality(Qt::WindowModality::ApplicationModal);
    m_windowFrame->setWindowButtonType(BaseWidget::ETitleBar::Close);
    m_windowFrame->setWindowFlags(m_windowFrame->windowFlags() | Qt::Dialog);
    m_windowFrame->setWindowIcon(QIcon());
    //m_windowFrame->setStatusBar(nullptr);
    m_windowFrame->setAttribute(Qt::WA_DeleteOnClose);
    m_windowFrame->setTitleBarHeight(24);
    m_windowFrame->setTitleFontSize(60);
    m_iconPathList = QString(":/rc/dialog_information.png,:/rc/dialog_warning.png,:/rc/dialog_error.png,:/rc/dialog_information.png,:/rc/dialog_information.png").split(",");

    m_textBrower_content->setStyleSheet("QTextBrowser { border: none; }");
    m_textBrower_content->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    ui->horizontalLayout_2->addWidget(m_textBrower_content);

    connect(this, &QDialog::rejected, [this]() { closeWindow((int)EDialogMsgResult::First); });
    connect(m_windowFrame, &IdaMainWindow::signal_closeButtonClick, this, &IdaDialogMsg::slot_closeButtonClick);
    QTimer::singleShot(0, this, &IdaDialogMsg::adjustSize);

    IdaWidgetHelperInstance->setMainButtonStyle(ui->pushButton_second);   // 将第二个设置为主按钮
}

/// @brief 设置对话框信息

void IdaDialogMsg::setDialogInfo(QString content, EDialogMsg dialogType, QString title, QStringList buttonTextList)
{
    m_dialogType = dialogType;
    m_textBrower_content->setText(content);
    m_textBrower_content->setFocus();
    //m_textBrower_content->adjustSize();
    m_windowFrame->setWindowTitle(title.isEmpty() ? QStringLiteral("警告") : title);
    if ((int)dialogType && (int)dialogType <= m_iconPathList.size())
    {
        ui->label_icon->setPixmap(QPixmap(m_iconPathList.at((int)dialogType - 1)));
    }

    if (1 == buttonTextList.size()
        || EDialogMsg::Normal == dialogType
        || EDialogMsg::Warning == dialogType
        || EDialogMsg::Error == dialogType)
    {
        ui->pushButton_second->setText(1 == buttonTextList.size() ? buttonTextList.at(0) : QStringLiteral("确定"));
        ui->pushButton_first->setVisible(false);
        ui->pushButton_second->setVisible(true);
        ui->pushButton_third->setVisible(false);
    }
    else if (2 == buttonTextList.size()
        || EDialogMsg::Question == dialogType)
    {
        ui->pushButton_first->setText(2 == buttonTextList.size() ? buttonTextList.at(0) : QStringLiteral("取消"));
        ui->pushButton_second->setText(2 == buttonTextList.size() ? buttonTextList.at(1) : QStringLiteral("确定"));
        ui->pushButton_first->setVisible(true);
        ui->pushButton_second->setVisible(true);
        ui->pushButton_third->setVisible(false);
    }
    else if (3 == buttonTextList.size())
    {
        ui->pushButton_first->setText(buttonTextList.at(0));
        ui->pushButton_second->setText(buttonTextList.at(1));
        ui->pushButton_third->setText(buttonTextList.at(2));
        ui->pushButton_first->setVisible(true);
        ui->pushButton_second->setVisible(true);
        ui->pushButton_third->setVisible(true);
    }

}

void IdaDialogMsg::showWindow()
{
    if (this == nullptr)
    {
        qDebug() << QStringLiteral("窗体已关闭，内存已释放！%1.").arg(this->metaObject()->className());
    }

    auto _screen = qApp->screenAt(this->geometry().center());
    if (_screen)
    {
        auto _windowRect = this->geometry();
        _windowRect.moveCenter(_screen->geometry().center());
        m_windowFrame->move(_windowRect.topLeft());
        m_windowFrame->adjustSize();
        m_windowFrame->show();
        m_windowFrame->adjustSize();
    }
    else
    {
        qDebug() << QStringLiteral("获取屏幕失败！");
    }
}

/// @brief 关闭窗体

void IdaDialogMsg::closeWindow(int status)
{
    m_windowFrame->hide();
    m_loop.exit(status);
}

/// @brief 事件循环,窗体阻塞

EDialogMsgResult IdaDialogMsg::execDialog(QString content, EDialogMsg dialogType, QString title, QStringList buttonTextList)
{
    setDialogInfo(content, dialogType, title, buttonTextList);
    showWindow();
    return (EDialogMsgResult)m_loop.exec();
}

void IdaDialogMsg::exitExec()
{
    if (m_loop.isRunning())
    {
        m_loop.exit(0);
    }
}

EDialogMsgResult IdaDialogMsg::showDialog(QWidget* parent, QString content, EDialogMsg dialogType, QString title, QStringList buttonTextList)
{
    if (!m_dialogMsgGlobal)
    {
        m_dialogMsgGlobal = new IdaDialogMsg();
    }
    else
    {
        m_dialogMsgGlobal->setVisible(true);
    }
    m_dialogMsgGlobal->resetParent(parent);
    return m_dialogMsgGlobal->execDialog(content, dialogType, title, buttonTextList);
}

void IdaDialogMsg::resetParent(QWidget* parent)
{
    m_windowFrame->setParent(parent, m_windowFrame->windowFlags());
}


void IdaDialogMsg::on_pushButton_first_clicked()
{
    closeWindow((int)EDialogMsgResult::First);
}

void IdaDialogMsg::on_pushButton_second_clicked()
{
    closeWindow((int)EDialogMsgResult::Second);
}

void IdaDialogMsg::on_pushButton_third_clicked()
{
    closeWindow((int)EDialogMsgResult::Third);
}

void IdaDialogMsg::slot_closeButtonClick()
{
    qDebug() << QStringLiteral("对话框窗体关闭 ： %1.").arg(this->metaObject()->className());

    bool _isAllowClose = true;
    // emit IdaDialogMsg::signal_readyCloseDialog(_isAllowClose);
    if (_isAllowClose)
    {
        m_windowFrame->hide();
        if (m_loop.isRunning())
            m_loop.exit(0);
    }
}

int IdaDialogMsg::getDialogType()
{
    return (int)m_dialogType;
}

